package com.itqf.controller;

import com.alibaba.fastjson.JSON;
import com.itqf.entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/11/16
 * @Time: 上午11:40
 */
@Controller  //find
public class JSONController {


    /**
     * 传统方式
     * @param response
     */
    @RequestMapping("/userInfo")
    public void  show(HttpServletResponse response){
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/json;charset=utf-8");
            Users users = new Users();
            users.setUsername("张三");
            users.setAge(18);
            String  json = JSON.toJSONString(users);
            response.getWriter().print(json);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 框架方式
     * @return
     */
    @RequestMapping("/userInfo1")
   // @ResponseBody //把方法的返回值转json   底层默认使用的jackson
    public  @ResponseBody Users show(){
        Users users = new Users();
        users.setUsername("张三");
        users.setAge(18);
        users.setSalary(18888.8888);
        users.setVersion(1);
        users.setBirthday(new Date());
      return  users;

    }


}
